//
//  BDJEssenceViewController.m
//  BuDeJie
//
//  Created by Guo Rui on 2017/9/19.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "BDJEssenceViewController.h"
#import "UIBarButtonItem+item.h"

#import "BDJAllViewController.h"
#import "BDJVoiceViewController.h"
#import "BDJVideoTableViewController.h"
#import "BDJPictureTableViewController.h"
#import "BDJJokeTableViewController.h"

static const CGFloat kTitleBarH = 44.f;

@interface BDJEssenceViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) UIView *titleBar;
@property (weak, nonatomic) UIScrollView *contentScrollView;
@property (weak, nonatomic) UIButton *previousTitleButton;
@property (weak, nonatomic) UIView *underline;

@property (strong, nonatomic) NSMutableArray *titleButtons;
@end

@implementation BDJEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupNavBar];
    
    // 1. 创建titleBar
    [self setupTitleBar];
    
    // 2. 创建contentScrollView
    [self setupContentScrollView];
    
    // 3. 添加子控制器
    [self setupChildViewControllers];
    
    [self titleButtonClick:self.titleButtons.firstObject];
}

- (void)setupTitleBar
{
    // 1. 创建titleBar
    UIView *titleBar = [[UIView alloc] init];
    [self.view addSubview:titleBar];
    self.titleBar = titleBar;
    
    titleBar.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:.5f];
    titleBar.frame = CGRectMake(0, 64.f, BDJScreenW, kTitleBarH);
    
    // 2. 向titleBar中添加titleButton
    [self setupTitleButtons];
    
    // 3. 向titleBar中添加underline
    [self setupUnderline];
}

- (void)setupContentScrollView
{
    UIScrollView *contentScrollView = [[UIScrollView alloc] init];
    [self.view addSubview:contentScrollView];
    self.contentScrollView = contentScrollView;
    
    CGFloat y = CGRectGetMaxY(self.titleBar.frame);
    CGFloat h = BDJScreenH - y;
    contentScrollView.frame = CGRectMake(0, y, BDJScreenW, h);
    
    contentScrollView.contentSize = CGSizeMake(self.titleButtons.count * BDJScreenW, h);
    contentScrollView.backgroundColor = [UIColor yellowColor];
    contentScrollView.pagingEnabled = YES;
    contentScrollView.showsHorizontalScrollIndicator = NO;
    contentScrollView.bounces = NO;
    contentScrollView.delegate = self;
    contentScrollView.scrollsToTop = NO;
}

- (void)setupTitleButtons
{
    NSArray *titles = @[@"全部", @"声音",  @"视频", @"图片", @"段子"];
    NSInteger count = titles.count;
    for (int i = 0; i < count; i++) {
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.titleBar addSubview:titleButton];
        [self.titleButtons addObject:titleButton];
        
        CGFloat w = BDJScreenW / count;
        CGFloat x = i * w;
        CGFloat h = self.titleBar.height;
        titleButton.frame = CGRectMake(x, 0, w, h);
        titleButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        [titleButton setTitle:titles[i] forState:UIControlStateNormal];
        [titleButton setTitle:titles[i] forState:UIControlStateSelected];
        [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [titleButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        
        [titleButton addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)setupChildViewControllers
{
    [self addChildViewController:[[BDJAllViewController alloc] init]];
    [self addChildViewController:[[BDJVoiceViewController alloc] init]];
    [self addChildViewController:[[BDJVideoTableViewController alloc] init]];
    [self addChildViewController:[[BDJPictureTableViewController alloc] init]];
    [self addChildViewController:[[BDJJokeTableViewController alloc] init]];
}

- (void)titleButtonClick:(UIButton *)titleButton
{
    if (self.previousTitleButton == titleButton) {
        [[NSNotificationCenter defaultCenter] postNotificationName:BDJTitleButtonDidRepeatClickNotification object:nil];
    }
    
    [self selectTitleButton:titleButton];
    
    NSInteger index = [self.titleButtons indexOfObject:titleButton];
    // 将scorllView移动到当前位置
    [self.contentScrollView setContentOffset:CGPointMake(index * self.contentScrollView.width, 0) animated:NO];
    
    // 将vc的view加入到contentView中
    [self addViewOfViewControllerToScrollView:index];
}

- (void)addViewOfViewControllerToScrollView:(NSInteger)index
{
    UIViewController *targetViewController = [self.childViewControllers objectAtIndex:index];
    
    if (targetViewController.isViewLoaded) {
        return;
    }
    
    targetViewController.view.frame = self.contentScrollView.bounds;
    [self.contentScrollView addSubview:targetViewController.view];
}

- (void)selectTitleButton:(UIButton *)titleButton
{
    // 选中按钮
    self.previousTitleButton.selected = NO;
    titleButton.selected = YES;
    self.previousTitleButton = titleButton;
    
    // 移动underline
    CGFloat titleButtonTitleWidth = [titleButton.currentTitle sizeWithAttributes:@{ NSFontAttributeName : titleButton.titleLabel.font }].width;
    [UIView animateWithDuration:.2 animations:^{
        self.underline.width = titleButtonTitleWidth;
        self.underline.centerX = titleButton.centerX;
    }];
    
    // 处理状态栏可点击
//    NSInteger index = [self.titleButtons indexOfObject:titleButton];
//    for (int i = 0; i < self.childViewControllers.count; i++) {
//        
//        UIViewController *vc = self.childViewControllers[i];
//        if (!vc.isViewLoaded) {
//            continue;
//        }
//        
//        UIScrollView *scrollView = (UIScrollView *)vc.view;
//        if (![scrollView isKindOfClass:[UIScrollView class]]) {
//            continue;
//        }
//        
//        scrollView.scrollsToTop = (i == index);
//    }
}

- (void)setupUnderline
{
    UIView *underline = [[UIView alloc] init];
    [self.titleBar addSubview:underline];
    self.underline = underline;
    
    underline.backgroundColor = [UIColor redColor];
    CGFloat h = 1.5f;
    CGFloat y = self.titleBar.height - h;
    underline.frame = CGRectMake(0, y, 0, h);
}

- (void)setupNavBar
{
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"nav_item_game_iconN"] highlightImage:[UIImage imageNamed:@"nav_item_game_click_icon"] target:self action:@selector(game)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"navigationButtonRandomN"] highlightImage:[UIImage imageNamed:@"navigationButtonRandom"] target:self action:@selector(random)];
    
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
}


- (NSMutableArray *)titleButtons
{
    if (!_titleButtons) {
        _titleButtons = [NSMutableArray arrayWithCapacity:0];
    }
    return _titleButtons;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x / scrollView.width;
    
    // 将titleBar中的titleButton选中
    // underline跟着移动
    UIButton *titleButton = [self.titleButtons objectAtIndex:page];
    [self selectTitleButton:titleButton];
    
    // 将ViewController的view放到上面
    [self addViewOfViewControllerToScrollView:page];
}

- (void)game
{
    BDJLog(@"%s", __func__);
}

- (void)random
{
    BDJLog(@"%s", __func__);
}

@end
